<template>
    <div class="common-layout">
        <el-row>
            <el-button @click="corr()" type="primary" plain>corr</el-button>
        </el-row>
        <el-row>
            <el-table :data="state.tableData" 
            :cell-class-name="tableColumnClassName"
            @sort-change="sortChange"
            :default-sort="state.sort"
            @header-click="tableHeaderClick"
            style="width: 100%" height="800">
                <el-table-column prop="date" fixed label="Date" width="120" sortable/>
                <el-table-column v-for="stock in state.stocks" width="160" :key="stock" :label="stock">
                    <template #default="scope">
                        <div v-if="scope.row[stock]">
                            <el-text class="mx-1" size="large">
                            {{ scope.row[stock].lp }}
                            </el-text>
                            <el-text class="mx-1" type="danger" v-if="scope.row[stock].monthPct >= 0" size="small">({{ scope.row[stock].monthPct }})</el-text>
                            <el-text class="mx-1" type="success" v-if="scope.row[stock].monthPct < 0" size="small">({{ scope.row[stock].monthPct }})</el-text>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
    </div>
</template>
  
<script setup>
import { reactive } from 'vue'
import { getMultiMonthData } from '../service/month';
import { openNewTableCompare, openNewTableWeekDetail } from '../router'
import { getLpClassName } from '../utils/help';

const base = "MultiMonthLpDetail"
const sortPropKey = base + "SortProp"
const sortOrderKey = base + "SortOrder"

const state = reactive({
    raw_data: null,
    tableData: [],
    list_data : [],
    options: [],
    sort: { 
        prop: sessionStorage.getItem(sortPropKey) || 'date' , 
        order: sessionStorage.getItem(sortOrderKey) || 'descending' 
    }
})

const prop = defineProps({
    stocks: String
})

const sortChange = (column, prop, order) => {
    sessionStorage.setItem(sortPropKey, column.prop)
    sessionStorage.setItem(sortOrderKey, column.order)
}

const loadData = () => {
    getMultiMonthData(prop.stocks).then(res => {
        state.raw_data = res.data

        state.stocks = Object.keys(state.raw_data)

        // 最大记录数
        let maxRecord = 0
        let maxStock = ''
        state.stocks.forEach(stock => {
            if (maxRecord < state.raw_data[stock].length) {
                maxRecord = state.raw_data[stock].length
                maxStock = stock
                //dates = state.raw_data[stock].map(item => item.date)
            }
        })

        let tableDataMap = new Map()
        // 设置初始值
        state.raw_data[maxStock].forEach(item => {
            let tmp = {} 
            tmp['date'] = item.date 
            state.stocks.forEach(stock => {
                tmp[stock] = null
            })
            tableDataMap.set(item.date,  tmp)
        })

        state.stocks.forEach(stock => {
            state.raw_data[stock].forEach(item => {
                tableDataMap.get(item.date)[stock] = item
            })
        })
        state.tableData = Array.from(tableDataMap.values())
    })
}

loadData()

const tableColumnClassName = ({row, column, rowIndex, columnIndex}) => {
    if (row['date'] == state.selected) {
        return 'lp-warning-row'
    }

    let cell = column.label
    let item = row[cell]
    if (item){
        return getLpClassName(item.lp)
    }
}

const tableHeaderClick = (column, event) => {
    let stock = column.label
    openNewTableWeekDetail(stock)
}

const corr = () => {
    openNewTableCompare(state.stocks)
}
</script>

<style scope>
.lp-warning-row {
    background: rgb(215, 246, 248);
}
</style>